package cn.bocweb.spinning.util;

import android.app.Activity;
import android.app.Application;


import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.bocweb.spinning.model.Bean.UserBean;

/**
 * Created by chenchangxing on 16/1/7.
 */
public class MyApplication extends Application {
    public static Map<String, Long> map;  //用来保存activity销毁时,验证码的时间
    private static MyApplication instance;
    private List<Activity> list;
    UserBean.DataEntity bean;

    public UserBean.DataEntity getBean() {
        return bean;
    }

    public void setBean(UserBean.DataEntity bean) {
        this.bean = bean;
    }

    public static MyApplication getInstance () {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        list = new LinkedList<>();
        bean = new UserBean.DataEntity();
        bean.readInfoFromLocal(this);


    }

    public void addActivity(Activity activity) {
        boolean bexit = false;
        for (Activity a : list) {
            if (a.equals(activity)) {
                bexit = true;
                break;
            }
        }
        if (!bexit) {
            list.add(activity);
        }
    }

    public void moveActivity(Activity activity) {
        boolean bexit = false;
        for (Activity a : list) {
            if (a.equals(activity)) {
                bexit = true;
                break;
            }
        }
        if (bexit) {
            list.remove(activity);
            activity.finish();
        }
    }

    public boolean isExistsActivity (Activity theAct) {
        boolean bIsExists = false;
        try {
            for (Activity activity : list) {
                if (activity != null) {
                    if (activity.equals(theAct)) {
                        bIsExists = true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bIsExists;
    }

    public void exit () {
        try {
            for (Activity activity : list) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }
}
